﻿Public Class frmMTipoServicio
    Public arrConsulta(2) As String
    Private arrCampos() As String = {"tiposervicio_id", "tiposervicio_nombre", "tiposervicio_descri"}

    Private Sub frmMTipoServicio_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mdiPrincipal.tsmiTipoDeServicio.Enabled = True
    End Sub

    Private Sub frmTipoIdentificacion_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.refreshList()
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbNuevo.Click
        Me.tsbNuevo.Enabled = False
        renderForm("Nuevo - Tipo de Servicio", 1)
    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbConsultar.Click
        Me.tsbConsultar.Enabled = False
        renderForm("Consultar - Tipo de Servicio", 2)
    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBorrar.Click
        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar - Tipoo de Servicio")

        If (objResult = DialogResult.Yes) Then
            Dim strId As String = Me.dgvTipoServicio.Rows(Me.dgvTipoServicio.CurrentRow.Index).Cells(0).Value.ToString

            mdlCRUD.delRecord("tiposervicio", "tiposervicio_id = '" & strId & "'")

            If (mdlCRUD.blnFlag = True) Then
                Me.refreshList()
            End If
        End If
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEditar.Click
        Dim strId As String = Me.dgvTipoServicio.Rows(Me.dgvTipoServicio.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then
            Me.tsbEditar.Enabled = False
            renderForm("Editar - Tipo de Servicio", 3)
        Else
            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Tipo de Servicio")
        End If
    End Sub

    Private Sub tsbRecargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRecargar.Click
        arrConsulta(0) = ""
        arrConsulta(1) = ""
        arrConsulta(2) = ""
        Me.refreshList()
    End Sub

    Private Sub renderForm(ByVal strTitle As String, ByVal intMode As Integer)
        Dim frmTipoServicio As New frmTipoServicio

        frmTipoServicio.MdiParent = mdiPrincipal
        frmTipoServicio.frmParent = Me

        frmTipoServicio.modeForm = intMode
        frmTipoServicio.Text = strTitle
        frmTipoServicio.Show()
    End Sub

    Public Sub refreshList()
        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("Tipo Servicio")

        For i As Integer = 0 To 2
            If (Me.arrConsulta(i) <> "") Then
                strCondicion &= IIf(strCondicion <> "", " AND ", "") & "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'"
            End If
        Next

        mdlCRUD.loadRecord(dstRecord, "Tipo Servicio", "tiposervicio", strCondicion, Nothing)

        Me.dgvTipoServicio.DataSource = dstRecord.Tables("Tipo Servicio")
        Me.dgvTipoServicio.Columns(0).HeaderText = "Id"
        Me.dgvTipoServicio.Columns(1).HeaderText = "Nombre"
        Me.dgvTipoServicio.Columns(2).HeaderText = "Nota"
        Me.dgvTipoServicio.Columns(0).Width = 60
        Me.dgvTipoServicio.Columns(1).Width = 100
        Me.dgvTipoServicio.Columns(2).Width = 300
    End Sub
End Class